﻿using System;
using System.Text;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;
using DataObject.Product;

namespace KMT.Controls.Common.Article
{
    public partial class Categories : System.Web.UI.UserControl
    {
        readonly StringBuilder _sb = new StringBuilder();
        public int TypeId { get; set; }
        public int CategoryId { get; set; }
        protected int ProductId;
        protected int ArticleId;
        protected string ActiveMenu = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId = Utils.GetIdFromRequestString(Utils.CategoryId, CategoryId);
            ProductId = Utils.GetIdFromRequestString(Utils.ProductId, 0);
            ArticleId = Utils.GetIdFromRequestString(Utils.ArticleId, 0);

            if (CategoryId > 0)
            {
                try
                {
                    var category = CategoryController.Category_GetById(CategoryId);
                    if (category != null)
                    {
                        if (category.ParentId == 0)
                        {
                            if (category.Priority - 1 >= 0)
                                ActiveMenu = (category.Priority - 1).ToString();
                        }
                        else
                        {
                            var subCategory = CategoryController.Category_GetById(category.ParentId);
                            if (subCategory != null)
                            {
                                if (subCategory.Priority - 1 >= 0)
                                    ActiveMenu = (subCategory.Priority - 1).ToString();
                            }
                        }
                    }
                }
                catch { }
            }

            if (ProductId > 0)
            {
                try
                {
                    var product = ProductController.Product_GetById(ProductId);
                    if (product != null)
                    {
                        var category = CategoryController.Category_GetById(product.CategoryId);
                        CategoryId = product.CategoryId;

                        if (category.ParentId == 0)
                        {
                            if (category.Priority - 1 >= 0)
                                ActiveMenu = (category.Priority - 1).ToString();
                        }
                        else
                        {
                            var subCategory = CategoryController.Category_GetById(category.ParentId);
                            if (subCategory != null)
                            {
                                if (subCategory.Priority - 1 >= 0)
                                    ActiveMenu = (subCategory.Priority - 1).ToString();
                            }
                        }
                    }
                }
                catch { }
            }

            if (ArticleId > 0)
            {
                try
                {
                    var articleId = ArticleController.Article_GetById(ArticleId);
                    if (articleId != null)
                    {
                        var category = CategoryController.Category_GetById(articleId.CategoryId);
                        CategoryId = articleId.CategoryId;

                        if (category.ParentId == 0)
                        {
                            if (category.Priority - 1 >= 0)
                                ActiveMenu = (category.Priority - 1).ToString();
                        }
                        else
                        {
                            var subCategory = CategoryController.Category_GetById(category.ParentId);
                            if (subCategory != null)
                            {
                                if (subCategory.Priority - 1 >= 0)
                                    ActiveMenu = (subCategory.Priority - 1).ToString();
                            }
                        }
                    }
                }
                catch { }
            }

            switch (TypeId)
            {
                case (int)CategoryType.Product:
                    BuildMenuProduct();
                    break;
                case (int)CategoryType.Article:
                    BuildMenuArticle();
                    break;
                case (int)CategoryType.Static:
                    BuilMenuStatic();
                    break;
            }

            ltCategory.Text = _sb.ToString();
        }

        public void BuildMenuProduct()
        {
            var categories = CategoryController.Category_GetByParentId((int)CategoryType.Product, Constants.PARENT_ROOT, Constants.ACTIVE);
            if (categories.Count <= 0) return;

            foreach (var t in categories)
            {
                _sb.AppendFormat("<a class=\"menuheader expandable openheader\">{0}</a>", t.Name);

                var subCategory = CategoryController.Category_GetByParentId((int)CategoryType.Product, t.CategoryId, Constants.ACTIVE);
                if (subCategory.Count <= 0) continue;

                _sb.AppendFormat("<ul class=\"categoryitems c{0}\">", t.CategoryId);
                foreach (var ts in subCategory)
                {
                    _sb.AppendFormat(
                        ts.CategoryId == CategoryId
                            ? "<li><a href=\"{0}\" style=\"font-weight: bold; color: #ff4a4a\">{1}</a></li>"
                            : "<li><a href=\"{0}\">{1}</a></li>",
                        UrlHelpers.ListProductByCategory(ConvertUtility.ToInt32(ts.CategoryId), ts.Url),
                        ts.Name);
                }
                _sb.Append("</ul>");
            }
        }

        private void BuildMenuArticle()
        {
            var categories = CategoryController.Category_GetByParentId_Filter(false, (int)CategoryType.Article, Constants.PARENT_ROOT, Constants.ACTIVE);
            if (categories.Count <= 0) return;

            foreach (var t in categories)
            {
                _sb.AppendFormat(
                    t.CategoryId == CategoryId
                        ? "<a class=\"menuheader\" href=\"{1}\" style=\"font-weight: bold; color: #ff4a4a\">{0}</a>"
                        : "<a class=\"menuheader\" href=\"{1}\">{0}</a>", t.Name,
                    UrlHelpers.ListArticleByCategory(ConvertUtility.ToInt32(t.CategoryId), t.Url));

                //var subCategory = CategoryController.Category_GetByParentId((int)CategoryType.Article, t.CategoryId, Constants.ACTIVE);
                //if (subCategory.Count > 0)
                //{
                //    _sb.Append("<ul class=\"categoryitems\">");
                //    foreach (var ts in subCategory)
                //    {
                //        _sb.AppendFormat(
                //            ts.CategoryId == CategoryId
                //                ? "<li><a href=\"{0}\" style=\"font-weight: bold; color: #ff4a4a\">{1}</a></li>"
                //                : "<li><a href=\"{0}\">{1}</a></li>",
                //            UrlHelpers.ListArticleByCategory(ConvertUtility.ToInt32(ts.CategoryId), ts.Url),
                //            ts.Name);
                //    }
                //    _sb.Append("</ul>");
                //}
            }
        }

        public void BuilMenuStatic()
        {
            var categories = CategoryController.Category_GetByParentId_Filter(true, (int)CategoryType.Article, Constants.PARENT_ROOT, Constants.ACTIVE);
            if (categories.Count <= 0) return;

            foreach (CategoryObject t in categories)
            {
                _sb.AppendFormat("<a class=\"menuheader expandable\">{0}</a>", t.Name);

                var subCategory = CategoryController.Category_GetByParentId((int) CategoryType.Article, t.CategoryId,
                                                                            Constants.ACTIVE);
                if (subCategory.Count <= 0) continue;

                _sb.Append("<ul class=\"categoryitems\">");
                foreach (var ts in subCategory)
                {
                    _sb.AppendFormat(
                        ts.CategoryId == CategoryId
                            ? "<li><a href=\"{0}\" style=\"font-weight: bold; color: #ff4a4a\">{1}</a></li>"
                            : "<li><a href=\"{0}\">{1}</a></li>",
                        UrlHelpers.ArticleStatic((int)StaticType.Category,
                                                 ConvertUtility.ToInt32(ts.CategoryId),
                                                 ts.Name),
                        ts.Name);
                }
                _sb.Append("</ul>");
            }
        }
    }
}